/**
 * XMLParser.java
 */
package com.coffee.price.common;

import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author gundallove
 * @email gundallove@gmail.com
 */
public class XMLParser {
	
	private int rowCnt;
	private Document doc;
	private DocumentBuilder builder = null;
	
	public XMLParser(){
		
		try{
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			builder = factory.newDocumentBuilder();
			
		}catch(Exception e){}
	}
	
	/**
	 * xml 파서 생성자
	 * @param is
	 */
	public XMLParser(String url){
		
		try{
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			builder = factory.newDocumentBuilder();
			
			this.doc = this.read(requestHttpUrl(url));
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public XMLParser(BufferedInputStream bis){
		
		try{
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			builder = factory.newDocumentBuilder();
			
			this.doc = this.read(bis);
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * <pre>
	 * @param reader
	 * @return
	 * @throws Exception
	 * Document
	 * </pre>
	 */
	public void doc(BufferedInputStream bis) throws Exception {
		
		this.doc = builder.parse(bis);
	}
	
	/**
	 * <pre>
	 * @param reader
	 * @return
	 * @throws Exception
	 * Document
	 * </pre>
	 */
	public Document read(BufferedInputStream bis) throws Exception {
		
		return builder.parse(bis);
	}
	
	/**
	 * <pre>
	 * @return
	 * parser
	 * List<Map<String,String>>
	 * </pre>
	 */
	public List<Map<String,String>> brand(){
		
		Map<String,String> data = null;
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		try{
			
			Element root = doc.getDocumentElement();
			
			NodeList brand_code = root.getElementsByTagName("brand_code");
			NodeList brand_name = root.getElementsByTagName("brand_name");
			
			int index = brand_code.getLength();
			
			for(int i = 0 ; i < index ; i++){
				
				data = new HashMap<String, String>();
				data.put("brand_code", brand_code.item(i).getChildNodes().item(0).getNodeValue());
				data.put("brand_name", brand_name.item(i).getChildNodes().item(0).getNodeValue());
				
				list.add(data);
			}
			
			this.rowCnt = list.size();
			
		}catch(Exception e){}
		
		return list;
	}

	public List<Map<String,String>> price(){
		
		Map<String,String> data = null;
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		try{
			
			Element root = doc.getDocumentElement();
			
			NodeList brand_code = root.getElementsByTagName("brand_code");
			NodeList coffee_name = root.getElementsByTagName("coffee_name");
			NodeList coffee_price1 = root.getElementsByTagName("coffee_price1");
			NodeList coffee_price2 = root.getElementsByTagName("coffee_price2");
			
			int index = brand_code.getLength();
			
			for(int i = 0 ; i < index ; i++){
				
				data = new HashMap<String, String>();
				data.put("brand_code", brand_code.item(i).getChildNodes().item(0).getNodeValue());
				data.put("coffee_name", coffee_name.item(i).getChildNodes().item(0).getNodeValue());
				data.put("coffee_price1", coffee_price1.item(i).getChildNodes().item(0).getNodeValue());
				data.put("coffee_price2", coffee_price2.item(i).getChildNodes().item(0).getNodeValue());
				
				list.add(data);
			}
			
			this.rowCnt = list.size();
			
		}catch(Exception e){}
		
		return list;
	}
	
	public List<Map<String,String>> discount(){
		
		Map<String,String> data = null;
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		try{
			
			Element root = doc.getDocumentElement();
			
			NodeList brand_code = root.getElementsByTagName("brand_code");
			NodeList discount = root.getElementsByTagName("discount_name");
			
			int index = brand_code.getLength();
			
			for(int i = 0 ; i < index ; i++){
				
				data = new HashMap<String, String>();
				data.put("brand_code", brand_code.item(i).getChildNodes().item(0).getNodeValue());
				data.put("discount_name", discount.item(i).getChildNodes().item(0).getNodeValue());
				
				list.add(data);
			}
			
			this.rowCnt = list.size();
			
		}catch(Exception e){}
		
		return list;
	}

	/**
	 * 
	 * @return
	 */
	public String version(){
		
		String version = "1";
		try{
			
			Element root = doc.getDocumentElement();
			
			NodeList version_numver = root.getElementsByTagName("version_numver");
			version = version_numver.item(0).getChildNodes().item(0).getNodeValue();
						
		}catch(Exception e){}
		
		return version;
	}
	
	/**
	 * 
	 * @return
	 */
	public Map<String,String> notice(){
		
		Map<String,String> notice = null;
		
		try{
			
			Element root = doc.getDocumentElement();
			
			NodeList seq = root.getElementsByTagName("seq");
			NodeList name = root.getElementsByTagName("name");
			NodeList date = root.getElementsByTagName("date");
			NodeList contents = root.getElementsByTagName("contents");
		
			notice = new HashMap<String, String>();
			notice.put("seq", seq.item(0).getChildNodes().item(0).getNodeValue());
			notice.put("name", name.item(0).getChildNodes().item(0).getNodeValue());
			notice.put("date", date.item(0).getChildNodes().item(0).getNodeValue());
			notice.put("contents", contents.item(0).getChildNodes().item(0).getNodeValue());
							
		}catch(Exception e){}
		
		return notice;
	}
	
	/**
	 * <pre>
	 * node 값
	 * @return
	 * String
	 * </pre>
	 */
	public String getNodeValue(String key , Element e){
		
		String value = "";
		Element elm = (Element)e.getElementsByTagName(key).item(0);
		if(elm.hasChildNodes()) value = elm.getFirstChild().getNodeValue();
		
		return value;
	}
	
	/**
	 * <pre>
	 * node 값
	 * @param key
	 * @param e
	 * @return
	 * List<String>
	 * </pre>
	 */
	public String[] getNodeValues(String key , Element e){
		
		NodeList node = e.getElementsByTagName(key);
		int ex_len = node.getLength();
		
		String[] list = new String[ex_len];
		for(int i = 0 ; i < ex_len ; i++)
			list[i] = node.item(i).getFirstChild().getNodeValue();
		
		return list;
	}
	
	public BufferedInputStream requestHttpUrl(String httpUrl){
		
		URL url = null;
		HttpURLConnection conn = null;
		BufferedInputStream br = null;
		
		try{
			url = new URL(httpUrl);
			conn = (HttpURLConnection)url.openConnection();
			conn.setConnectTimeout(800);
			br = new BufferedInputStream(conn.getInputStream());
			
			return br;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * @return the rowCnt
	 */
	public int getRowCnt() {
		return rowCnt;
	}
	
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
